package com.lots.ai.controller;

import com.lots.ai.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;


@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class ChatController {
    private final ChatClient chatClient;
    private final ChatMemory chatMemory;
    private final ChatHistoryRepository chatHistoryRepository;

//    @RequestMapping(value = "/chat", produces =
//            "text/html;charset=UTF-8")
//    public Flux<String> chat(@RequestParam(defaultValue = "你是谁？")
//                             String prompt) {
//        return chatClient
//                .prompt(prompt)
//                .stream() // 流式调用
//                .content();
//    }

    @RequestMapping(value = "/chat", produces =
            "text/html;charset=UTF-8")
    public Flux<String> chat(@RequestParam(defaultValue = "讲个笑话 ") String prompt, String chatId) {
            chatHistoryRepository.addChatId(chatId);
            return chatClient
            .prompt(prompt)
            .advisors(a ->
                    a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
            .stream()
            .content();
}
}



